using System;
using System.Collections.Generic;
using System.Linq;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Model = Microsoft.Xna.Framework.Graphics.Model;
using GoblinXNA;
using GoblinXNA.SceneGraph;
using GoblinXNA.Graphics;
using GoblinXNA.Graphics.Geometry;



namespace SplashOn_PhoneLib
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class SplashOn_WelcomeScreen
    {  
        static bool[] if_int_highlight = new bool[5];

        // Base Rectangle Bar
        public Rectangle welcomeRectangle = new Rectangle(0, 0, 640, 80);
        public Rectangle helpRectangle = new Rectangle(0, 0, 640, 480);
        public Rectangle helpIconRectangle = new Rectangle(550, 300, 80, 80);
        public Rectangle baseRectangle = new Rectangle(120, GraphicsDeviceManager.DefaultBackBufferHeight - 25, 560, 25);
        
        static Rectangle[] screenRect = new Rectangle[5];
        static String[] screenDisplay = new String[5];

        public Model objectModel;
        public Model cylinderModel;
        public Model coneModel;
        public Model torusModel;
        public Model sphereModel;
        public Color splashColor = Color.Purple;
        float aspectRatio;
        private Texture2D baseTexture;
        private SpriteFont displayFontBold;
        private SpriteFont displayFont;

        public SplashOn_WelcomeScreen() { }

        public void Initialize()
        {
           // vidPlayer = new VideoPlayer();
            screenDisplay[0] = "COLOR ";
            screenDisplay[1] = " GAME ";
            screenDisplay[2] = "OBJECT";
            screenDisplay[3] = " MAP ";
            screenDisplay[4] = " HELP";

            for (int i = 0; i < 5; i++)
            {
                screenRect[i] = new Rectangle(baseRectangle.X + (i * 112), GraphicsDeviceManager.DefaultBackBufferHeight - 35, 112, 35);
                if_int_highlight[i] = false;
            }

           
        }

        public void setColorToSplash(Color color)
        {
            foreach (ModelMesh mesh in objectModel.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.DiffuseColor = Color.Blue.ToVector3();
                }
                mesh.Draw();
            }
        }

        public void setObject(String name)
        {
            DebugHelper.w("setObject = " + name);
            if (name == "Cylinder")
            {
                objectModel = cylinderModel;
            }
            else if(name == "Torus")
            {
                objectModel = torusModel;
            }
            else if(name == "Cone")
            {
                objectModel = coneModel;
            }
        }

        public void createObjects(float rotateCamera)
        {            
            // Set the position of the model in world space, and set the rotation.
            Vector3 modelPosition = new Vector3(70f, 0f, 0.0f);    
            // Set the position of the camera in world space, for our view matrix.
            Vector3 cameraPosition = new Vector3(70f, 0.0f, 80.0f);
            Matrix[] transforms = new Matrix[objectModel.Bones.Count];
            objectModel.CopyAbsoluteBoneTransformsTo(transforms);
            
            foreach (ModelMesh mesh in objectModel.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();                    
                    effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationX(rotateCamera) * Matrix.CreateTranslation(modelPosition);                    
                    effect.View = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);
                    effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(90.0f), aspectRatio, 1.0f, 10000.0f);
                }
                mesh.Draw();
            }    
        }

        public void createSplash()
        {
            // Set the position of the model in world space, and set the rotation.
            Vector3 modelPosition = new Vector3(-70f, -10f, 0.0f);
            // Set the position of the camera in world space, for our view matrix.
            Vector3 cameraPosition = new Vector3(-70f, -10.0f, 80.0f);
            Matrix[] transforms = new Matrix[objectModel.Bones.Count];
            sphereModel.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in sphereModel.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.DiffuseColor = splashColor.ToVector3();
                    effect.EnableDefaultLighting();
                    effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationX(0.001f) * Matrix.CreateTranslation(modelPosition);
                    effect.View = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);
                    effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(90.0f), aspectRatio, 1.0f, 10000.0f);
                }
                mesh.Draw();
            }
        }
       
        public void LoadContent(ContentManager content)
        {
            cylinderModel = content.Load<Model>("cylinder");
            torusModel = content.Load<Model>("torus");
            coneModel = content.Load<Model>("cone");
            objectModel = torusModel;
            sphereModel = content.Load<Model>("sphere");
            aspectRatio = State.Device.Viewport.AspectRatio;
            baseTexture = content.Load<Texture2D>("baserectangle");
            displayFont = content.Load<SpriteFont>("DisplayFont");
            displayFontBold = content.Load<SpriteFont>("DisplayFontBold");
        }

        public void VideoHelp(SpriteBatch spritbatch)
        {
           // spritbatch.Draw(vidTexture, vidRectangle, Color.White);
        }

        public void BaseDisplay( int int_highlight)
        {
            //spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
            //splashon_welcome.BaseDisplay(spriteBatch, int_highlight);
            //spriteBatch.End();

            State.SharedSpriteBatch.Begin();
            for (int i = 0; i < 5; i++)
            {

                if (int_highlight == i)
                {
                    if (!if_int_highlight[int_highlight])
                    {
                        screenRect[int_highlight].Inflate(0, 6);
                        if_int_highlight[int_highlight] = true;
                    }
                    State.SharedSpriteBatch.Draw(baseTexture, screenRect[i], Color.Gray);
                    State.SharedSpriteBatch.DrawString(displayFontBold, screenDisplay[i], new Vector2(baseRectangle.X + (i * 112) + 10, baseRectangle.Y + 3),
                                                                   Color.Green);
                }
                else
                {
                    if_int_highlight[i] = false;
                    screenRect[i] = new Rectangle(baseRectangle.X + (i * 112), baseRectangle.Y, 112, 35);
                    State.SharedSpriteBatch.Draw(baseTexture, screenRect[i], Color.LightGray);
                    State.SharedSpriteBatch.DrawString(displayFont, screenDisplay[i], new Vector2(baseRectangle.X + (i * 112) + 10, baseRectangle.Y + 3),
                                                                            Color.Black);
                }
            }
            ///GraphicsDevice.SetDirectRenderingMode(true);
           
            //State.SharedSpriteBatch.GraphicsDevice.set
            State.SharedSpriteBatch.End();
        }

        public void OpenPage(SpriteFont font, Texture2D helpTexture, SpriteBatch spritbatch, int i)
        {
            welcomeRectangle = new Rectangle(welcomeRectangle.X, welcomeRectangle.Y - 1, welcomeRectangle.Width, welcomeRectangle.Height);
            helpRectangle = new Rectangle(helpRectangle.X, helpRectangle.Y + 40, helpRectangle.Width, helpRectangle.Height);            
            spritbatch.Draw(helpTexture, helpRectangle, Color.White);          
        }        

        public void MainScreen(SpriteFont font, Texture2D helpTexture, SpriteBatch spritbatch)
        {
            helpRectangle = new Rectangle(0, 0, 640, 480);
            spritbatch.Draw(helpTexture, helpRectangle, Color.White);          
        }
        
    }
}

